package com.android.jr_infraredcamera

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.BitmapFactory
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbDeviceConnection
import android.hardware.usb.UsbInterface
import android.hardware.usb.UsbManager
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import com.android.jr_infraredcamera.base.BaseActivity
import com.android.jr_infraredcamera.databinding.ActivityMainBinding
import com.android.jr_infraredcamera.utils.Config
import com.android.jr_infraredcamera.utils.UsbPermissionHelper
import com.taopao.androidnginxrtmp.service.NginxUtils
import com.vmark.ffmpeglib.Ffmpeg
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {

    companion object {
        const val TAG = "===MainActivity==="
    }

    // private lateinit var usbPermissionHelper: UsbPermissionHelper
    override fun initData() {

        // usbPermissionHelper = UsbPermissionHelper(this)
        // usbPermissionHelper.registerReceiver()

        val vendorId = 2760
        val productId = 13344

        // 尝试查找目标设备
        //  val device = usbPermissionHelper.findTargetDevice(vendorId, productId)
        //   if (device != null) {
        // 请求授权，系统会弹出授权提示框
        //   usbPermissionHelper.requestPermission()
        // } else {
        //     Log.e("MainActivity", "目标 USB 设备未找到")
        //  }

//        Log.e(TAG, "initData: ===========")

//        supportFragmentManager.beginTransaction()
//            .replace(R.id.mMainFragment, UsbRtmpFragment.newInstance("", "")).commit()

        supportFragmentManager.beginTransaction()
            .replace(R.id.mMainFragment, UccCameraFragment.newInstance("", "")).commit()

//       supportFragmentManager.beginTransaction()
//           .replace(R.id.mMainFragment, UsbCameraFragment.newInstance("", "")).commit()
    }


    override fun initView() {

        CoroutineScope(Dispatchers.IO).launch {
            if (Config.isNginxStart) {
                NginxUtils.startNginx(this@MainActivity)
            }
            if (Config.isPushStream) {
                Log.e(TAG, "initView: =====PushStream  init" )
                delay(5000)
                Ffmpeg.getInstance().initUpStreamer()
                Ffmpeg.getInstance().pushStream()
            }

        }


    }

    override fun initObservable() {

    }

    override fun onDestroy() {

        //  usbPermissionHelper.unregisterReceiver()
        CoroutineScope(Dispatchers.IO).launch {
            if (Config.isNginxStart) {
                NginxUtils.stopNginx(this@MainActivity)
            }
            if (Config.isPushStream) {
                Ffmpeg.getInstance().cleanupStreamer()
            }

        }
        super.onDestroy()
    }
}